home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / TABLW.ICN < prev    next >
Text File  |  1992-09-28  |  3KB  |  93 lines

  1. ############################################################################
  2. #
  3. #    File:     tablw.icn
  4. #
  5. #    Subject:  Program to tabulate words in a file
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     December 27, 1989
  10. #
  11. ###########################################################################
  12. #  
  13. #     This program tabulates words and lists number of times each
  14. #  word occurs. A word is defined to be a string of consecutive
  15. #  upper- and lowercase letters with at most one interior occurrence
  16. #  of a dash or apostrophe.
  17. #  
  18. #  Options: The following options are available:
  19. #  
  20. #       -a   Write the summary in alphabetical order of the words.
  21. #            This is the default.
  22. #  
  23. #       -i   Ignore case distinctions among letters; uppercase
  24. #            letters are mapped into to corresponding lowercase
  25. #            letters on input. The default is to maintain case dis-
  26. #            tinctions.
  27. #  
  28. #       -n   Write the summary in numerical order of the counts.
  29. #  
  30. #       -l n Tabulate only words longer than n characters. The
  31. #            default is to tabulate all words.
  32. #  
  33. #       -u   Write only the words that occur just once.
  34. #  
  35. ############################################################################
  36. #
  37. #  Links: options, usage
  38. #
  39. ############################################################################
  40.  
  41. link options, usage
  42.  
  43. global limit, icase
  44.  
  45. procedure main(args)
  46.    local wcount, unique, order, s, pair, lwidth, rwidth, max, opts, l, i
  47.  
  48.    limit := 0                # lower limit on usage to list
  49.    unique := 0                # switch to list unique usage only
  50.    order := 3                # alphabetical ordering switch
  51.  
  52.    opts := options(args,"ail+nu")
  53.    if \opts["a"] then order := 3
  54.    if \opts["n"] then order := 4
  55.    if \opts["u"] then unique := 1
  56.    if \opts["i"] then icase := 1
  57.    l := \opts["l"] | 1
  58.    if l <= 0 then Usage("-l needs positive parameter")
  59.  
  60.    wcount := table(0)            # table of words
  61.    every wcount[words()] +:= 1
  62.    wcount := sort(wcount,order)
  63.    if unique = 1 then {
  64.       while s := get(wcount) do
  65.          if get(wcount) = 1 then write(s)
  66.       }
  67.    else {
  68.       max := 0
  69.       rwidth := 0
  70.       i := 1
  71.       while i < *wcount do {
  72.          max <:= *wcount[i]
  73.          rwidth <:= *wcount[i +:= 1]
  74.      }
  75.       lwidth := max + 3
  76.       while write(left(get(wcount),lwidth),right(get(wcount),rwidth))
  77.       }
  78. end
  79.  
  80. #  generate words
  81. #
  82. procedure words()
  83.    local line, word
  84.    while line := read() do {
  85.       if \icase then line := map(line)
  86.       line ? while tab(upto(&letters)) do {
  87.          word := tab(many(&letters)) || ((tab(any('-\'')) ||
  88.             tab(many(&letters))) | "")
  89.          if *word > limit then suspend word
  90.          }
  91.       }
  92. end
  93.